package com.java3_8;

public class Dish {
    private int totalDish;

    public Dish() {
    }

    public Dish(int totalDish) {
        this.totalDish = totalDish;
    }

    public int getTotalDish() {
        return totalDish;
    }

    public void setTotalDish(int totalDish) {
        this.totalDish = totalDish;
    }

    public void addDish() {
        synchronized (this) {
            while (totalDish >= 5) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            totalDish++;
            System.out.println(Thread.currentThread().getName() + "  刚做好了一盘菜，现在一个有" + totalDish + "盘菜");
            this.notifyAll();;
        }

    }

    public void delDish() {
        synchronized (this) {
            while(totalDish<=0){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            totalDish--;
            System.out.println(Thread.currentThread().getName() + "  刚端走了一盘菜，现在一个有" + totalDish + "盘菜");
            this.notifyAll();
        }
    }
}
